﻿/// <reference path="/Scripts/Base/jquery-1.7.1.js" />
/// <reference path="/Scripts/Base/jquery.easing.min.js" />

$(function () {

    var selectorContainerSuffix = "selector-container";
    var dateSelectorPrefix = "date-";
    var timeSelectorPrefix = "time-";

    var filterFromSelector = "filter-date-from";
    var filterToSelector = "filter-date-to";


    function ParseFilterDateString(dateString) {
        var dateParts = dateString.split("#");
        var date = new Date(
            dateParts[2],
            dateParts[1] - 1,
            dateParts[0],
            dateParts[3],
            dateParts[4],
            0, 0);
        //debugger;
        return date;
    }

    function GetFilterRangeValueSelected(rangeSelectorObject) {
        //debugger;
        //lekérjük mind a kiválasztott, mind a default értéket
        var defaultValue = rangeSelectorObject.find(".range-default");
        var selectedValue = rangeSelectorObject.find(".range-selected");
        var result = '';
        //ha a default érték jelen van
        if (defaultValue.length > 0) {
            result = defaultValue.text();
        } else {
            //ha a kiválasztott érték jelen van
            if (selectedValue.length > 0) {
                result = selectedValue.text();
            } else { alert("no value from range value selector"); }
        }
        result = result.replace("\n", '').trim();
        //debugger;
        return result;
    }

    function GetFilterDateFromSelector(container, timeSelPrefix, dateSelPrefix, hour, min) {
        //get begin date
        var dateAsArray = GetFilterRangeValueFromSelector(container, dateSelPrefix);
        //get begin time
        //var timeAsArray = GetFilterRangeValueFromSelector(container, timeSelPrefix);

        //debugger;
        if (dateAsArray.length == 3) {
            var dateFromArrays = new Date(dateAsArray[0], dateAsArray[1] - 1, dateAsArray[2], hour, min);
            //debugger;
            return dateFromArrays;
        }
    }

    function GetFilterRangeValueFromSelector(containerDivId, selectorTypePrefix) {
        var resultArray = new Array();
        try {
            var childFilter = "." + selectorTypePrefix + selectorContainerSuffix + " ." + selectorContainerSuffix;
            //alert(childFilter);
            $("#" + containerDivId).find(childFilter).each(function () {
                //get range default or range value selected
                var result = GetFilterRangeValueSelected($(this));
                if (typeof result != "undefined") {
                    resultArray.push(parseInt(result));
                }
            });
        } catch (e) {
            debugger;
        }

        return resultArray;
    }

    $(".log-filter-input .styled-select select option").click(function () {
        $("#log-filter-entry-text").focus();
    });

    $("#btn-do-filter").click(function () {
        //filter tulajdonságainak összegyűjtése
        //a filterek egymás után történnek applikálásra
        var type = $("#filter-log-part-type").val();
        var isTypeChecked = $("#filter-log-part-type").parents(".log-filter-input").find(".check-container").attr("data-checked") === 'true';
        if (isTypeChecked) {
            //leszűrjük az elérhető bejegyzéseket azokra, melyekben megtalálható az adott típus
            $(".log-entry").each(function () {
                if ($(this).find(".entry-part .entry-part-type-id[value=\"" + type + "\"]").length < 1) {
                    $(this).hide({
                        duration: 500,
                        effect: "fade",
                    });
                } else {
                    if (!$(this).is(":visible")) {
                        $(this).show({
                            duration: 500,
                            effect: "fade",
                        });
                    }
                }
            });
        }
        var isEntryTextChecked = $("#filter-input-entry-text").parents(".log-filter-input").find(".check-container").attr("data-checked") === 'true';
        var filterText = $("#log-filter-entry-text").val();
        if (isEntryTextChecked && filterText != '') {
            var filterTextSplit = filterText.split(' ');
            //szűrés szövegre
            $(".log-entry").each(function () {
                var foundText = false;
                $(this).find(".part-description").each(function () {

                    var lower = $(this).text().toLowerCase();
                    for (var i = 0; i < filterTextSplit.length; i++) {
                        if (lower.indexOf(filterTextSplit[i].toLowerCase()) != -1) {
                            foundText = true;
                        }
                    }
                });
                if (!foundText) {
                    $(this).hide({
                        duration: 500,
                        effect: "fade",
                    });
                } else {
                    if (!$(this).is(":visible")) {
                        $(this).show({
                            duration: 500,
                            effect: "fade",
                        });
                    }
                }

            });
        }

        var isToDateChecked = $("#filter-date-to").find(".check-container").attr("data-checked") === 'true';
        var isFromDateChecked = $("#filter-date-from").find(".check-container").attr("data-checked") === 'true';
        if (isFromDateChecked) {
            var dateFrom = GetFilterDateFromSelector(filterFromSelector, timeSelectorPrefix, dateSelectorPrefix, 0, 0);
            FilterLogentriesByDate(dateFrom, false);
            //debugger;
        }
        if (isToDateChecked) {
            var dateTo = GetFilterDateFromSelector(filterToSelector, timeSelectorPrefix, dateSelectorPrefix, 23, 59);
            FilterLogentriesByDate(dateTo, true);
            //debugger;
        }
    });

    function FilterLogentriesByDate(dateToFilter,lessThen) {
        $(".log-entry").each(function () {
            var toDateRaw = $(this).find(".log-entry-end-date").attr("data-date");
            if (typeof toDateRaw != "undefined" && toDateRaw != '') {
                var _date = ParseFilterDateString(toDateRaw);
                if (typeof _date != "undefined") {
                    var check = false;
                    if (lessThen) {
                        check = _date > dateToFilter;
                    } else {
                        check = _date < dateToFilter;
                    }
                    if (check) {
                        $(this).hide({
                            duration: 500,
                            effect: "fade",
                        });
                    } else {
                        if (!$(this).is(":visible")) {
                            $(this).show({
                                duration: 500,
                                effect: "fade",
                            });
                        }
                    }
                }
                else {
                    debugger;
                }
            }
            else {
                debugger;
            }
        });
    }

    $("#btn-clear-filter").click(function () {
        $(".log-entry").show();
    });
});